package com.windrift.springhibernate.controller;

import com.windrift.springhibernate.model.Asset;
import com.windrift.springhibernate.service.AssetService;

import com.windrift.springhibernate.model.AssetType;
import com.windrift.springhibernate.service.AssetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xuweigui
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping(value = "/asset")
public class AssetController {

    @Autowired
    private AssetService assetService;
    @Autowired
    private AssetTypeService assetTypeService;

    @RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public void delete(@PathVariable("id") Integer id ) {
        assetService.remove(id);
    }


    @RequestMapping()
    public String execute(Model model) {
        List<Asset> assets = assetService.findAll();
        model.addAttribute("assets", assets);

        List<AssetType> assetTypes = assetTypeService.findAll();
        StringBuilder sb = new StringBuilder();
        sb.append("assetTypes:[");
        for (AssetType assetType : assetTypes) {
            sb.append(assetType.toJSON()).append(",");
        }
        sb.append("]");
        return "accounting/AssetList";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    //TODO
    public String add(@RequestParam("assetTypeId") Integer assetTypeId,
                      @RequestParam("costTypeId") Integer costTypeId,
                      @RequestParam("costDate") Date costDate,
                      @RequestParam("amount") Double amount,
                      @RequestParam("comment") String comment) {

        Asset asset = new Asset();
        asset.setAssetTypeId(assetTypeId);
        //asset.set
        //assetService.add(asset);
        return null;
    }


    public void setAssetService(AssetService assetService) {
        this.assetService = assetService;
    }


    public void setAssetTypeService(AssetTypeService assetTypeService) {
        this.assetTypeService = assetTypeService;
    }


}
